package com.wyh.blog.controller;


import com.wyh.blog.entity.Article;
import com.wyh.blog.entity.Imgs;
import com.wyh.blog.service.impl.ArticleServiceImpl;
import com.wyh.blog.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/articleController")
public class ArticleController {

    @Autowired
    private ArticleServiceImpl articleService;
    @Autowired
    private UserServiceImpl userService;


    @GetMapping("/getIssuePage")
    public String getIssuePage(){

        return "issue";
    }
//
//    @GetMapping("/upLoadContent")
//    public String upLoadText(String content, String title,Model model){
//        System.out.println("进来了");
//        System.out.println(content);
//        articleService.addText(content,title);
//        model.addAttribute("text",content);
//        return "home";
//
//    }

    @GetMapping("/details")
    public String Details(String id ,Model model){
        int aid = Integer.parseInt(id);

        System.out.println(articleService.getText(aid));
        Article article = articleService.getText(aid);
        model.addAttribute("tit",article.getTit());
        model.addAttribute("content",article.getContent());
        model.addAttribute("update",article.getUpdateTime());


//        System.out.println(articleList.get(Integer.parseInt(id)).getTit());
        List<Article> articleList = userService.article();
        model.addAttribute("articleList",articleList);


        return "text";
    }


    /**
     * 解析富文本
     */
    @PostMapping("/upLoadContent")
    public void upLoadContent(Imgs imgs, String content, String title, Model model, HttpServletResponse response) throws IOException {
        System.out.println("!23123");
        System.out.println(content);
//        去掉前台文本域中内容的html标签
//        String str = (String) contents;
//        String tocontents = str.replaceAll("<[.[^>]]*>","");
        // 1) 获取文件信息
        List<MultipartFile> files =  imgs.getImages();
        // 2) 判断是否有文件上传
        if( null != files && files.size() >0 ){
            System.out.println("asasdas");
            // 3) 扩展使用，用于多个文件上传
            List<String> fileNames  = new ArrayList<>();
            // 4) 遍历需上传的文件个数
            for (MultipartFile multipartFile: files){
                // 5) 获取文件名
                String fileName = multipartFile.getOriginalFilename();
                // 5.1) 验证是否上传的是图片，验证其他文件一样方式，如果去掉便通用
                // if(fileName.endsWith("jpg") || fileName.endsWith("gif") || fileName.endsWith("png"))

                // 6) 将文件名添加至filNames
                fileNames.add(fileName);
                // 7) 构建文件存储位置和文件
                File imageFile = new File("d://imgs",fileName);
                // 8) 判断构建的路径是否存在，如果不存在就创建一个
                if (!imageFile.getParentFile().exists()) {
                    imageFile.getParentFile().mkdirs();
                }
                try {
                    // 9) 将文件上传到目标目录下
                    multipartFile.transferTo(imageFile);
                    System.out.println(imageFile);
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        articleService.addText(content,title);

        model.addAttribute("text",content);

        response.sendRedirect("/userController/getHome");
    }



}
